package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dto.Login;
import com.example.dto.Register;
import com.example.dto.interview;
import com.example.dto.result;
import com.example.mapper.UserMapper;
import com.example.pojo.*;
import com.example.service.TeacherService;
import com.example.service.UserService;
import com.example.utils.FileUtils;
import com.example.utils.JwtUtils;
import com.example.utils.Md5Utils;
import com.example.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TeacherService teacherService;

    //用户注册
    @PostMapping("/register")
    public result register(@RequestBody Register register){
        //查询用户
        user u = userService.findByUserName(register.getUsername());
        if(u==null){
            //注册
            userService.register(register);
            return result.success();
        }
        else{
            //占用
            return result.error("用户已存在!");
        }
    }

    //用户登录
    @PostMapping("/login")
    public result login(@RequestBody Login login){
        //根据用户名查询用户
        user u = userService.findByUserName(login.getUsername());
        //判断用户是否存在
        if(u!=null){
            //判断密码是否正确
            if(Md5Utils.getMd5String(login.getPassword()).equals(u.getPassword())){
                //登录成功
                Map<String ,Object> claims = new HashMap<>();
                claims.put("GraduateID",u.getGraduateID());
                claims.put("username",u.getName());
                claims.put("roles",1);//普通用户
                String token = JwtUtils.genToken(claims);
                return result.success(token);
            }
            return result.error("密码错误!");
        }
        //查找管理员
        teacher t = teacherService.findByName(login.getUsername());
        if(t!=null){
            //判断密码是否正确
            if(login.getPassword().equals(t.getPassword())){
                //登录成功
                Map<String ,Object> claims = new HashMap<>();
                claims.put("ID",t.getID());
                claims.put("username",t.getUsername());
                claims.put("roles",0);//管理员
                String token = JwtUtils.genToken(claims);
                return result.success(token);
            }
            return result.error("密码错误!");
        }


        return result.error("用户不存在!");
    }

    //获取登录用户信息
    @GetMapping("/userInfo")
     public result<user> userInfo(){
        Map<String,Object> map = ThreadLocalUtils.get();
        String username = (String) map.get("username");
        user u = userService.findByUserName(username);
        return result.success(u);
     }

    //更新用户信息
    @PutMapping("/update")
    public result update(@RequestBody user u){
        if(userMapper.findByUserName(u.getName())!=null){
            userService.update(u);
            return result.success(u);
        }
        return result.error("没有找到这个人，无法更新");
    }

    //修改密码
    @PutMapping("/updatePwd")
    public result updatePwd(@RequestBody Login l){
        String pwd = Md5Utils.getMd5String(l.getPassword());
        String username = l.getUsername();
        userService.updatePwd(pwd,username);
        return result.success();
    }

    //上传头像
    @PostMapping("/uploadPic")
    public result uploadPic(MultipartFile file) throws IOException{
        //把文件内容存储到本地
        String url = FileUtils.uploads(file);
        return result.success(url);
    }

    //获取用户岗位申请信息
    @GetMapping("/applyInfo")
    public result applyInfo(@RequestParam Integer page, @RequestParam Integer size,@RequestParam String id){
        Page<applyInfo> applyInfoPage = new Page<>(page,size);
        IPage<applyInfo> res = userService.applyInfo(applyInfoPage,id);
        return result.success(res);
    }

    //插入用户岗位申请信息
    @PostMapping("/apply")
    public result apply(@RequestBody interview i){
        Map<String,Object> map = ThreadLocalUtils.get();
        String id = (String) map.get("username");
        applyInfo a = new applyInfo();
        a.setEmployerName(i.getName());
        a.setPositionName(i.getPositionName());
        a.setSalary(i.getSalary());
        a.setStatus("已申请");
        a.setStudentID(id);
        userService.apply(a);
        return result.success();
    }
}
